package advice;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Component
@Aspect //表示这是一个切面 (切点+通知)
public class CacheAdvice {

    //定义切点（切点表达式查找）
    @Pointcut("execution(* service.impl..*.*(..))")
    public void pt(){}

    //前置通知
    @Before("pt()")
    public void before(JoinPoint jp){
        Object[] args = jp.getArgs();
        System.out.println("目标方法的参数：" + Arrays.toString(args));

        Signature signature = jp.getSignature();
        System.out.println("目标方法的签名：" + signature);

        System.out.println("before~~");
    }

    @After("pt()")
    public void after(){
        System.out.println("after~~");
    }

    @AfterReturning(value = "pt()",returning = "res")
    public void afterReturning(Object res){
        System.out.println("afterReturning~~");
        System.out.println("目标方法的返回值：" + res);
    }

    @AfterThrowing(value = "pt()",throwing = "err")
    public void afterThrowing(Throwable err){
        System.out.println("afterThrowing~~");
        System.out.println("目标方法的异常信息：" + err.getMessage());
    }

//    @Around("pt()")
    public Object around(ProceedingJoinPoint pjp) {

        try {
            System.out.println("around-before...");
            //目标方法
            Object proceed = pjp.proceed();

            System.out.println("around-afterReturning...");

            return proceed;
        } catch (Throwable e) {
            e.printStackTrace();
            System.out.println("around-afterThrowing...");
        }finally {
            System.out.println("around-after...");
        }

        return null;
    }
}